﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.BusinessLogic.Provider;
using OpenSoftStore.Orm.Static;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMoney
{
    public partial class AddMoney : UserControl
    {

        private  T_Money Money;
        public V_Money Vmoney;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public AddMoney()
        {
            InitializeComponent();
            Money = new T_Money();
            Vmoney = new V_Money();
            if (cmbAccountId != null)
            {

                var mengylogic = new MoneyLogic();
                var accountdata = mengylogic.SqlWhere<T_Account>("1=1 Order by AddTime desc").ToList();
                //var lst = accountdata.Select(v => v.Name).ToList();
                cmbAccountId.DataSource = accountdata;
                cmbAccountId.DisplayMember = "Name";
                cmbAccountId.ValueMember = "Id";
            }
        }

        private void AddMoney_Load(object sender, EventArgs e)
        {
            rdSpend.Focus();
            //rdIncome.Select();
        }

        public void GetMoney(string id)
        {
            var moneyLogic = new MoneyLogic();
            Vmoney = moneyLogic.GetMoneyView(" where T_Money.Id ='" + id + "'")[0];
            Money.Id = Vmoney.Id;

            if (Vmoney.Type=="收入") rdIncome.Select();
            else rdSpend.Select();
            if (Vmoney.Money < 0) Vmoney.Money = - Vmoney.Money;
            numMoney.Value = Vmoney.Money;
            txtRemark.Text = Vmoney.Remark;
            txtType.Text = Vmoney.TreeName;
            cmbAccountId.SelectedValue = Vmoney.AccountId;
            dtAddTime.Value = Vmoney.AddTime;
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }


        private void btSelType_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            string code = "10.";
            if (!rdIncome.Checked) code = "11.";
            formTreeSelecter.GetTreeByType("Money", code);
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;
            Money.TreeId = formTreeSelecter.TypeId;
            txtType.Text = formTreeSelecter.TypeText;
        }



        private void BtnSave_Click(object sender, EventArgs e)
        {
            var moneyLogic = new MoneyLogic();
            Money.AddTime = dtAddTime.Value;
            Money.Remark = txtRemark.Text.Trim();
            Money.PayType = cmbAccountId.Text.Trim();
            Money.AccountId = cmbAccountId.SelectedValue.ToString();
            Money.ShopId = S_Admin.ShopId;
            Money.AdminId = S_Admin.Id;
            Money.Money = decimal.Parse(numMoney.Text.Trim());
            if (Money.Money == 0)
            {
                FormSysMessage.ShowMessage("请输入正确金额！");
                numMoney.Select();
                numMoney.Focus();
                return;
            }
            Money.Type = rdIncome.Checked ? "收入" : "支出";
            if (string.IsNullOrEmpty(txtType.Text))
            {
                Money.ObjectId2 = rdIncome.Checked ? "收入" : "支出";
                Money.TreeId = rdIncome.Checked ? "564173af-810f-4dd4-81cc-06d2461c9718" : "f3cae141-ab51-4a45-88be-d969a162413f";
            }
            else
                Money.ObjectId2 = txtType.Text;


            if (moneyLogic.SaveMoney(Money) == 0) return;

            MoneyList.Instance.button1_Click_1(null, null);
            var findForm = FindForm();
            if (findForm != null) findForm.Close();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void rdIncome_CheckedChanged(object sender, EventArgs e)
        {
            txtType.Text = "";
            Money.TreeId = "";
        }

        private void rdSpend_CheckedChanged(object sender, EventArgs e)
        {
            txtType.Text = "";
            Money.TreeId = "";
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
    }
}
